Structure
in C is like a custom data container that lets you bundle different types of variables together under one label. It helps in organizing and managing related information in your program. Each piece of data inside the structure has its own name and type, making it easier to work with and understand complex data relationships.
Syntax of Structure
struct
structure_name
{
data_type
member1
;
data_type
member2
;
;
Declaring and Accessing Structures
You can declare variables of a structure type in the same way you declare variables of basic data types.
struct
Student
stud1
;
strcpy
(
stud1.name
, "Alice");
stud1.age
= 20;
stud1.gpa
= 3.8;
printf
("Student Name: %s
",
stud1.name
);
printf
("Age: %d
",
stud1.age
);
printf
("GPA: %.2f
",
stud1.gpa
);
Why use Structures
1.
Grouping Related Data:
Allow you to group together variables of different data types under a single name. This is useful when dealing with entities that have multiple attributes. For example, a Person structure might include fields for name, age, and address.
2.
Memory Allocation and Alignment:
Structures help in efficient memory allocation and alignment. The members of a structure are stored in contiguous memory locations, making it easier to manage and access the data.
3.
Enhanced Readability:
Using structures makes the code more readable and self-explanatory. Instead of dealing with individual variables scattered throughout the code, you can encapsulate related variables within a structure, making the code easier to understand.
4.
Passing and Returning Complex Data:
Structures enable you to pass and return complex data structures to and from functions. This is particularly useful when you want to work with entities that have multiple attributes without having to pass each attribute separately.
5.
Code Modularity:
Structures contribute to code modularity by encapsulating related data and functionality within a single unit. This makes it easier to maintain and update code since changes to the structure can be isolated.
Example of Structure
#include
<stdio.h>
Copy Code
struct
Point
{
int
x
;
int
y
;
};
int
main
() {
struct
Point
p1
;
p1.x
= 10;
p1.y
= 5;
printf
("Point coordinates: (%d, %d)
",
p1.x
,
p1.y
);
return
0;
}
Output
Point coordinates: (10, 5)
Nesting structures in C
Nesting structures in C involves defining a structure within another structure. This allows you to represent hierarchical relationships or group related data in a more organized manner.
Example of Nesting Structures
#include
<stdio.h>
Copy Code
struct
Address
{
char
street
[50];
char
city
[30];
int
zip_code
;
;
struct
Person
{
char
name
[50];
int
age
;
struct
Address
address
;
;
int
main
() {
struct
Person
person1
;
strcpy
(
person1.name
, "John Doe");
person1.age
= 25;
strcpy
(
person1.address.street
, "123 Main St");
strcpy
(
person1.address.city
, "Anytown");
person1.address.zip_code
= 12345;
printf
("
Person's name: %s
",
person1.name
);
printf
("
Person's age: %d
",
person1.age
);
printf
("
Person's address:
");
printf
("
Street: %s
",
person1.address.street
);
printf
("
City: %s
",
person1.address.city
);
printf
("
ZIP Code: %d
",
person1.address.zip_code
);
return
0;
}
Output
Person's name: John Doe
Person's age: 25
Person's address:
Street: 123 Main St
City: Anytown
ZIP Code: 12345
In this
example
,
structures
are used to represent a person's
information
and
address
. The Person
structure
includes a
nested
Address structure. A variable, person1, is
declared
and
initialized
, and values are assigned to both structures'
members
. The program then displays the person's details,
demonstrating
how nesting structures enhances the modeling of real-world
relationships
, especially in
complex
data structures.